package com.xiaoxing.salesclient.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;
import com.xiaoxing.salesclient.di.component.DaggerSopLiuChengComponent;
import com.xiaoxing.salesclient.di.module.SopLiuChengModule;
import com.xiaoxing.salesclient.mvp.contract.SopLiuChengContract;
import com.xiaoxing.salesclient.mvp.presenter.SopLiuChengPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.SopLiuChengAdapter;
import com.xiaoxing.salesclient.mvp.ui.entity.SopLiuChengList;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.StatusBarUtil;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SopLiuChengFragment extends BaseFragment<SopLiuChengPresenter> implements SopLiuChengContract.View {


    @BindView(R2.id.refreshLayout)
    RefreshLayout refreshLayout;

    @BindView(R2.id.empty)
    View mEmptyLayout;

    @BindView(R2.id.empty_text)
    TextView empty;

    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.title)
    TextView mTitle;

    @BindView(R2.id.btnRight)
    ImageView btnRight;

    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    private SopLiuChengAdapter mAdapter;
    private List<SopLiuChengList> mHuoDongListArrayList = new ArrayList<>();

    public static SopLiuChengFragment newInstance() {
        SopLiuChengFragment fragment = new SopLiuChengFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerSopLiuChengComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .sopLiuChengModule(new SopLiuChengModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sop_liu_cheng, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        btnRight.setImageDrawable(getResources().getDrawable(R.drawable.icon_sao_miao));
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArmsUtils.snackbarText(getString(R.string.sales_client_zheng_zai_kai_fa_zhong));
            }
        });
        mTitle.setText("SOP流程");
        initEmptyText();
        initToolbar();
        initRefreshLayout();
        initRecyclerView();
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(HuoDongListActivity.this, VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new SopLiuChengAdapter(getActivity(), null));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Utils.navigation(getActivity(), RouterHub.SALES_CLIENT_GU_KE_DETAIL_ACTIVITY);
            }
        });
    }

    private void initRefreshLayout() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMore(false);

        refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mHuoDongListArrayList.clear();
                mHuoDongListArrayList.addAll(loadModels());
                mAdapter.notifyDataSetChanged();
                mEmptyLayout.setVisibility(View.GONE);
                refreshLayout.finishRefresh();
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(0);
            }
        });
    }

    /**
     * 模拟数据
     */
    private List<SopLiuChengList> loadModels() {
        List<SopLiuChengList> addressLists = new ArrayList<>();
        int[] imgs = {
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
                R.drawable.img01,
        };

        for (int i = 0; i < 10; i++) {

            SopLiuChengList addressList = new SopLiuChengList();
            addressList.setImg_url(imgs[i]);
            addressLists.add(addressList);
        }


        return addressLists;
    }

    private void initToolbar() {
        toolbar.setNavigationIcon(null);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            }
//        });

        //状态栏透明和间距处理
        StatusBarUtil.immersive(getActivity());
        StatusBarUtil.setPaddingSmart(getActivity(), toolbar);
    }

    private void initEmptyText() {
        empty.setText("暫無消息,下拉更新");
    }

    private void getIndexData() {

    }

    @Override
    public void setData(@Nullable Object data) {
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
}
